home *** CD-ROM | disk | FTP | other *** search
Text File | 2001-02-20 | 17.4 KB | 860 lines | [AMAS/AMAP] |
- // -* 3SpaceLib.js *-
- //
- // Name: 3Space Library
- // Description: Core functions for 3Space behaviors
- // Author: Frederic Brunel, Mickael Gasrel
- // Version: $Id: 3SpaceLib.js,v 1.35 2000/12/29 10:12:02 consumer Exp $
- //
-
- //
- // Global variables
- //
- var browser = navigator.appName.toLowerCase () ;
- var gPlayer;
- var sceneId;
-
- var defaultPlayerName = "TSPlayer";
- var defaultSceneID = "TSScene";
- var init = "0";
-
- //
- // Library initialization.
- //
- function TSLibInit(playerName, sceneIdInit)
- {
- if (browser == "netscape") {
- gPlayer = document.embeds[playerName];
- }
- else {
- if (browser == "microsoft internet explorer") {
- gPlayer = document.all[playerName];
- }
- }
-
- sceneId = sceneIdInit;
-
- // The lib is now initialized
- init = "1";
- }
-
- var guid = 0;
-
- // Utility function:creates a unique name from a basename
- function TSMakeUniqID(baseName)
- {
- return baseName + "_" + guid++;
- }
-
- function TSGetSceneId()
- {
- return sceneId;
- }
-
- //
- // Nodes management.
- //
- function TSAppendChild(parentId, nodeId)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSAppendChild(parentId, nodeId);
- }
- else {
- error ("TSAppendChild", "3Space library not initialized");
- }
- }
-
- function TSCreateNode(nodeType, nodeId)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSCreateNode(nodeType, nodeId);
- }
- else {
- error ("TSCreateNode", "3Space library not initialized") ;
- }
- }
-
- function TSGetAttribute(nodeId, attributeName)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- var attributeValue = gPlayer.TSGetAttribute(nodeId, attributeName);
- if (attributeValue == "") {
- error ("TSGetAttribute", "Attribute '" + attributeName + "' not found in the node '" + nodeId + "'") ;
- }
- else {
- return attributeValue ;
- }
- }
- else {
- error ("TSGetAttribute", "3Space library not initialized") ;
- }
- }
-
- function TSRemoveNode(nodeId)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSRemoveNode (nodeId);
- }
- else {
- error ("TSRemodeNode", "3Space library not initialized") ;
- }
- }
-
- function TSSetAttribute(nodeId, attributeName, attributeValue)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSSetAttribute (nodeId, attributeName, attributeValue) ;
- }
- else {
- error ("TSSetAttribute", "3Space library not initialized") ;
- }
- }
-
- function TSSetSource(sourceFile, xmlId)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- if (browser == "netscape") {
- gPlayer.TSSetSource (sourceFile) ;
- }
- else if (browser == "microsoft internet explorer") {
- gPlayer.XMLID = xmlId ;
- }
- }
- else {
- error ("TSSetSource", "3Space library not initialized") ;
- }
- }
-
- function TSUpdateNode(nodeId)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSUpdateNode (nodeId) ;
- }
- else {
- error ("TSUpdateNode", "3Space library not initialized") ;
- }
- }
-
- function TSUpdateNodeAttribute(nodeId, attributeName, attributeValue)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSUpdateNodeAttribute (nodeId, attributeName, attributeValue) ;
- }
- else {
- error ("TSUpdateNodeAttribute", "3Space library not initialized") ;
- }
- }
-
- function TSGetExtraParam(nodeId, paramName)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- return gPlayer.TSGetExtraParam(nodeId, paramName);
- }
- else {
- error ("TSGetExtraParam", "3Space library not initialized") ;
- }
- }
-
- function TSSetExtraParam(nodeId, paramName, paramValue)
- {
- if (init == "0")
- TSLibInit(defaultPlayerName, defaultSceneID);
-
- if (gPlayer != null) {
- gPlayer.TSSetExtraParam(nodeId, paramName, paramValue);
- }
- else {
- error ("TSSetExtraParam", "3Space library not initialized") ;
- }
- }
-
- //
- // Miscellaneous.
- //
- function error (functionName, message)
- {
- alert("Error in function " + functionName + " :\n" + message);
- }
-
- //-----------------------------------------------------------------
- // Description: Basic maths
- //
-
- //
- // Conversions
- //
-
- function TSDegToRad(angleDeg)
- {
- return angleDeg * Math.PI / 180.0;
- }
-
- function TSRadToDeg(angleRad)
- {
- return angleRad / Math.PI * 180.0;
- }
-
- //
- // Point
- //
-
- function TSPoint(x, y, z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
-
- function TSMakePoint(x, y, z)
- {
- return new TSPoint(x, y, z);
- }
-
- function TSMakePointFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- var strSplit = str.split(" ");
- return new TSPoint(parseFloat (strSplit[0]), parseFloat (strSplit[1]), parseFloat (strSplit[2]));
- }
-
- function TSMakeStringFromPoint(pt)
- {
- return (pt.x + ' ' + pt.y + ' ' + pt.z);
- }
-
- function TSPointEqual(p1, p2)
- {
- return (TSPointDistance(p1, p2) == 0);
- }
-
- //
- // Point operations
- //
-
- function TSPointRotateX(pt, angle)
- {
- new_x = pt.x
- new_y = (pt.y * Math.cos(angle)) + (pt.z * Math.sin(angle))
- new_z = (pt.z * Math.cos(angle)) - (pt.y * Math.sin(angle))
-
- return new TSPoint(new_x, new_y, new_z)
- }
-
- function TSPointRotateY(pt, angle)
- {
- new_x = (pt.x * Math.cos(angle)) - (pt.z * Math.sin(angle))
- new_y = pt.y
- new_z = (pt.x * Math.sin(angle)) + (pt.z * Math.cos(angle))
-
- return new TSPoint(new_x, new_y, new_z)
- }
-
- function TSPointRotateZ(pt, angle)
- {
- new_x = (pt.x * Math.cos(angle)) + (pt.y * Math.sin(angle))
- new_y = (pt.y * Math.cos(angle)) - (pt.x * Math.sin(angle))
- new_z = pt.z
-
- return new TSPoint(new_x, new_y, new_z)
- }
-
- function TSPointSphericalRotate(theta, phi)
- {
- return TSSphericCoordsToVector(TSMakeSphericCoords(1.,parseFloat(theta),parseFloat(phi)));
- }
-
- function TSPointNegate(pt)
- {
- return TSMakePoint(-parseFloat(pt.x),
- -parseFloat(pt.y),
- -parseFloat(pt.z));
- }
-
- function TSPointTranslate(pt, x, y, z)
- {
- return TSMakePoint(parseFloat(pt.x) + parseFloat(x),
- parseFloat(pt.y) + parseFloat(y),
- parseFloat(pt.z) + parseFloat(z));
- }
-
- function TSPointScale(pt, x, y, z)
- {
- return TSMakePoint(parseFloat(pt.x) * parseFloat(x),
- parseFloat(pt.y) * parseFloat(y),
- parseFloat(pt.z) * parseFloat(z));
- }
-
- function TSPointMultiply(pt, c)
- {
- return TSMakePoint(parseFloat(pt.x) * parseFloat(c),
- parseFloat(pt.y) * parseFloat(c),
- parseFloat(pt.z) * parseFloat(c));
- }
-
- function TSPointDistance(pt1, pt2)
- {
- return TSVectorLength(TSMakeVector(pt1, pt2));
- }
-
- function TSPointInterpolate(pt1, pt2, alpha)
- {
- return TSMakePoint(parseFloat(pt1.x) * (1.0 - alpha) + parseFloat(pt2.x) * alpha,
- parseFloat(pt1.y) * (1.0 - alpha) + parseFloat(pt2.y) * alpha,
- parseFloat(pt1.z) * (1.0 - alpha) + parseFloat(pt2.z) * alpha);
- }
-
- //
- // Vector operations
- // (The vector structure is a point)
- //
-
- function TSMakeVector(pt1, pt2)
- {
- return TSMakePoint(pt2.x - pt1.x, pt2.y - pt1.y, pt2.z - pt1.z);
- }
-
- function TSMakeVectorFromString(str)
- {
- return TSMakePointFromString(str);
- }
-
- function TSMakeStringFromVector(v)
- {
- return TSMakeStringFromPoint(v);
- }
-
- function TSVectorMultiply(v, c)
- {
- return TSPointMultiply(v, c);
- }
-
- function TSVectorLength(v)
- {
- return Math.sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
- }
-
- function TSVectorNormalize(v)
- {
- return TSPointMultiply(v, 1 / TSVectorLength(v));
- }
-
- //
- // Bounding box
- //
-
- function TSBoundingBox(pointMin, pointMax)
- {
- this.min = pointMin;
- this.max = pointMax;
- }
-
- function TSMakeBoundingBox(pointMin, pointMax)
- {
- var bbox = new TSBoundingBox(pointMin, pointMax);
-
- // [HACK] Le constructeur n'a pas l'air de fonctionner!! :-(
- bbox.min = pointMin;
- bbox.max = pointMax;
-
- return bbox;
- }
-
- function TSMakeBoundingBoxFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- var strSplit = str.split(" ");
- var min = new TSPoint(strSplit[0], strSplit[1], strSplit[2]);
- var max = new TSPoint(strSplit[3], strSplit[4], strSplit[5]);
-
- return TSMakeBoundingBox(min, max);
- }
-
- function TSBoundingBoxGetLengthX(bbox)
- {
- return (bbox.max.x - bbox.min.x);
- }
-
- function TSBoundingBoxGetLengthY(bbox)
- {
- return (bbox.max.y - bbox.min.y);
- }
-
- function TSBoundingBoxGetLengthZ(bbox)
- {
- return (bbox.max.z - bbox.min.z);
- }
-
- //
- // Color
- //
-
- function TSColor(r, g, b)
- {
- this.r = r;
- this.g = g;
- this.b = b;
- }
-
- function TSMakeColor(r, g, b)
- {
- return new TSColor(r, g, b);
- }
-
- function TSMakeColorFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- strSplit = str.split(" ");
- return new TSColor(strSplit[0], strSplit[1], strSplit[2]);
- }
-
- function TSMakeStringFromColor(col)
- {
- return (col.r + ' ' + col.g + ' ' + col.b);
- }
-
- //
- // Color operations
- //
-
- function TSColorMultiply(col, c)
- {
- return TSMakeColor(parseFloat(col.r) * c,
- parseFloat(col.g) * c,
- parseFloat(col.b) * c);
- }
-
- //
- // Spherical coordinates
- //
-
- function TSSphericCoords(rho,theta,phi)
- {
- this.rho = rho;
- this.theta = theta;
- this.phi = phi;
- }
-
- function TSMakeSphericCoords(rho,theta,phi)
- {
- return new TSSphericCoords(rho,theta,phi);
- }
-
- function TSMakeSphericCoordsFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- var strSplit = str.split(" ");
- return new TSSphericCoords(strSplit[0], strSplit[1], strSplit[2]);
- }
-
- function TSMakeStringFromSphericCoords(pt)
- {
- return (pt.rho + ' ' + pt.theta + ' ' + pt.phi);
- }
-
- function TSVectorToSphericCoords(vector)
- {
- var rho,theta,phi;
- rho = TSVectorLength(vector);
-
- vector = TSVectorNormalize(vector);
-
- phi = Math.asin(vector.y) ;
-
- cosPhi = Math.cos(phi);
- if (Math.abs(cosPhi) < 0.00001) {
- cosPhi = 0.00001;
- }
- tmp = vector.z / cosPhi;
- if (tmp <= -1) {
- tmp = -0.99999;
- } else if (tmp >= 1) {
- tmp = 0.99999;
- }
- theta = Math.acos(tmp);
- if (vector.x < 0) {
- theta = -theta;
- }
-
- return TSMakeSphericCoords(rho, theta, phi);
- }
-
- function TSSphericCoordsToVector(sphericCoords)
- {
- fphi = parseFloat(sphericCoords.phi);
- ftheta = parseFloat(sphericCoords.theta);
-
- new_x = (Math.cos(fphi) * Math.sin(ftheta)) * sphericCoords.rho;
- new_y = (Math.sin(fphi) ) * sphericCoords.rho;
- new_z = (Math.cos(fphi) * Math.cos(ftheta)) * sphericCoords.rho;
-
- return TSMakePoint(new_x, new_y, new_z);
- }
-
-
- //-----------------------------------------------------------------
- // Description: Functions for accessing and building 3Space objects
- //
-
- function TSMakeSolid(id, fixed, position)
- {
- TSCreateNode("Solid", id);
-
- TSSetAttribute(id, 'fixed', fixed);
- TSSetAttribute(id, 'position', position);
- }
-
- function TSDropSolid(id)
- {
- TSUpdateNodeAttribute(id, 'fixed', '0');
- }
-
- function TSFreezeSolid(id)
- {
- TSUpdateNodeAttribute(id, 'fixed', '1');
- }
-
- function TSSolidGetPosition(solidName)
- {
- return TSMakePointFromString(TSGetAttribute(solidName, 'position'));
- }
-
- function TSSolidSetPosition(solidName, position)
- {
- TSUpdateNodeAttribute(solidName, 'position', position);
- }
-
- function TSSolidGetMass(solidName)
- {
- return parseFloat (TSGetAttribute(solidName, 'mass'));
- }
-
- function TSSolidGetBoundingBox(solidName)
- {
- return TSMakeBoundingBoxFromString(TSGetAttribute(solidName, 'boundingBox'));
- }
-
- function TSSolidGetLengthX(solidName)
- {
- return TSBoundingBoxGetLengthX(TSSolidGetBoundingBox(solidName));
- }
-
- function TSSolidGetLengthY(solidName)
- {
- return TSBoundingBoxGetLengthY(TSSolidGetBoundingBox(solidName));
- }
-
- function TSSolidGetLengthZ(solidName)
- {
- return TSBoundingBoxGetLengthZ(TSSolidGetBoundingBox(solidName));
- }
-
- function TSSolidGetMaxLength(solidName)
- {
- var bbox = TSSolidGetBoundingBox(solidName);
- var max = 0;
-
- if (TSBoundingBoxGetLengthX(bbox) > max)
- max = TSBoundingBoxGetLengthX(bbox);
-
- if (TSBoundingBoxGetLengthY(bbox) > max)
- max = TSBoundingBoxGetLengthY(bbox);
-
- if (TSBoundingBoxGetLengthZ(bbox) > max)
- max = TSBoundingBoxGetLengthZ(bbox);
-
- return max;
- }
-
- //
- // Geometry
- //
-
- function TSMakeBlock(id, color, lengthX, lengthY, lengthZ)
- {
- TSCreateNode("GeomBlock", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'lengthX', lengthX);
- TSSetAttribute(id, 'lengthY', lengthY);
- TSSetAttribute(id, 'lengthZ', lengthZ);
- }
-
- function TSMakeCone(id, length, radius, color)
- {
- TSCreateNode("GeomCone", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'length', length);
- TSSetAttribute(id, 'radius', radius);
- }
-
- function TSMakeCube(id, length, color)
- {
- TSCreateNode("GeomCube", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'length', length);
- }
-
- function TSMakeCylinder(id, length, radius, color)
- {
- TSCreateNode("GeomCylinder", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'length', length);
- TSSetAttribute(id, 'radius', radius);
- }
-
- function TSMakePlan(id, length, width, color)
- {
- TSCreateNode("GeomPlan", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'length', length);
- TSSetAttribute(id, 'width', width);
- }
-
- function TSMakeSphere(id, radius, color)
- {
- TSCreateNode("GeomSphere", id);
-
- TSMsphere.setAttribute(id, 'color', color);
- TSMsphere.setAttribute(id, 'radius', radius);
- }
-
- function TSMakeText(id, text, size, color)
- {
- TSCreateNode("GeomText", id);
-
- TSSetAttribute(id, 'color', color);
- TSSetAttribute(id, 'text', text);
- TSSetAttribute(id, 'size', size);
- }
-
- function TSMakeGeomURL(id, url, name)
- {
- TSCreateNode("GeomURL", id);
-
- TSSetAttribute(id, 'url', url);
- if (name != "")
- TSSetAttribute(id, 'name', name);
- }
-
- //
- // Collision volumes
- //
-
- function TSMakeVolBounding(id, type)
- {
- TSCreateNode("VolBounding", id);
-
- TSSetAttribute(id, 'boundingType', type);
- }
-
- //
- // Light
- //
-
- function TSMakeSolidPointLight(id, position, color, state)
- {
- TSCreateNode("SolidPointLight", id);
-
- TSSetAttribute(id,'fixed', '1');
- TSSetAttribute(id,'position', position);
- TSSetAttribute(id,'color', color);
- TSSetAttribute(id,'state', state);
- }
-
- function TSMakeSolidSpotLight(id, position, direction, cutOffAngle, dropOffAngle, color, state)
- {
- TSCreateNode("SolidSpotLight", id);
-
- TSSetAttribute(id,'fixed', '1');
- TSSetAttribute(id,'position', position);
- TSSetAttribute(id,'direction', direction);
- TSSetAttribute(id,'cutOffAngle', cutOffAngle);
- TSSetAttribute(id,'dropOffAngle', dropOffAngle);
- TSSetAttribute(id,'color', color);
- TSSetAttribute(id,'state', state);
- }
-
- //
- // Camera
- //
-
- function TSCameraGetPosition(id)
- {
- return TSSolidGetPosition(id);
- }
-
- function TSCameraGetTargetPosition(id)
- {
- return TSMakePointFromString(TSGetAttribute(id, 'targetPoint'));
- }
-
- function TSCameraLookAt(id, targetPoint)
- {
- TSUpdateNodeAttribute(id, 'targetPoint', targetPoint)
- }
-
- function TSCameraLookAtSolid(id, targetID)
- {
- TSUpdateNodeAttribute(id, 'target', targetID);
- }
-
- function TSCameraGetLookAtVector(id)
- {
- return TSMakeVector(TSCameraGetPosition(id), TSCameraGetTargetPosition(id));
- }
-
- //
- // Particle systems
- //
-
- function TSMakeParticleSystem(id,
- textureFile,
- emitterShape,
- position,
- rotation,
- yawVariation,
- pitchVariation,
- initialColor,
- endColor,
- initialSize,
- endSize,
- speed,
- speedVariation,
- lifeSpan,
- lifeSpanVariation)
- {
- TSCreateNode("ParticleSystem",id);
-
- TSSetAttribute(id, 'particleTexture', textureFile);
- TSSetAttribute(id, 'emitterShape', emitterShape);
- TSSetAttribute(id, 'position', position);
- TSSetAttribute(id, 'rotation', rotation);
- TSSetAttribute(id, 'yawVariation', yawVariation);
- TSSetAttribute(id, 'pitchVariation', pitchVariation);
- TSSetAttribute(id, 'initialColor', initialColor);
- TSSetAttribute(id, 'endColor', endColor);
- TSSetAttribute(id, 'initialSize', initialSize);
- TSSetAttribute(id, 'endSize', endSize);
- TSSetAttribute(id, 'speed', speed);
- TSSetAttribute(id, 'speedVariation', speedVariation);
- TSSetAttribute(id, 'lifeSpan', lifeSpan);
- TSSetAttribute(id, 'lifeSpanVariation', lifeSpanVariation);
- }
-
- //
- // Forces
- //
-
- function TSMakeDampingSolidForce(id, angular, linear)
- {
- TSCreateNode("DampingSolidForce", id);
-
- TSSetAttribute(id, 'angular', angular);
- TSSetAttribute(id, 'linear', linear);
- }
-
- function TSMakeSpringForce(id, constant, damping, point, target)
- {
- TSCreateNode("SpringForce", id);
-
- TSSetAttribute(id, 'constant', constant);
- TSSetAttribute(id, 'damping', damping);
- TSSetAttribute(id, 'point', point);
- TSSetAttribute(id, 'target', target);
- }
-
- function TSMakeDragForce(id, intensity, dragPoint, targetPoint)
- {
- TSCreateNode("DragForce", id);
-
- TSSetAttribute(id, 'intensity', intensity);
- TSSetAttribute(id, 'dragPoint', dragPoint);
- TSSetAttribute(id, 'targetPoint', targetPoint);
- }
-
- function TSMakeShootForce(id, direction, intensity, point)
- {
- TSCreateNode("ShootForce", id);
-
- TSSetAttribute(id, 'direction', direction);
- TSSetAttribute(id, 'intensity', intensity);
- TSSetAttribute(id, 'point', point);
- }
-
- //
- // Links
- //
-
- function TSMakeAxisLink(id, axis, point, target)
- {
- TSCreateNode("AxisLink", id);
-
- TSSetAttribute(id, 'axis', axis);
- TSSetAttribute(id, 'point', point);
- TSSetAttribute(id, 'target', target);
- }
-
- function TSMakeCylindricalLink(id, axis, point, target)
- {
- TSCreateNode("CylindricalLink", id);
-
- TSSetAttribute(id, 'axis', axis);
- TSSetAttribute(id, 'point', point);
- TSSetAttribute(id, 'target', target);
- }
-
- function TSMakeSphericalLink(id, point, target)
- {
- TSCreateNode("SphericalLink", id);
-
- TSSetAttribute(id,'point', point);
- TSSetAttribute(id,'target', target);
- }
-
- //
- // Events
- //
-
- function TSMakeTimerEvent(id, at, every, handler)
- {
- TSCreateNode("TimerEvent", id);
-
- TSSetAttribute(id, 'at', at);
- TSSetAttribute(id, 'every', every);
- TSSetAttribute(id, 'handler', handler);
- }
-